package com.liangwj.springbootweb.controller;

import com.github.pagehelper.PageInfo;
import com.liangwj.springbootweb.model.User;
import com.liangwj.springbootweb.services.IMybatisUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <b>Description:</b> mybatis user controller <br/>
 * @author Lingwenjun
 * @date 2019/1/3-13:58
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/mybatis")
public class MybatisUserController {

    @Autowired
    private IMybatisUserService mybatisUserService;

    @RequestMapping(value = "/new", method = RequestMethod.POST)
    public String createUser(User user) {

        try{
            mybatisUserService.createUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            return "create error!" + e.getMessage() + e.getCause();
        }
        return user.toString();
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public PageInfo<User> findAllUser(
            @RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10") int pageSize
            ) {

        return mybatisUserService.finaAllUserByPage(pageNum, pageSize);
    }
}
